package com.team.childapp.util;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Base64;

import androidx.core.content.FileProvider;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.Utils;
import com.team.childapp.R;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * 作者：suichengju
 * 时间：2024/7/22:20:12
 * 邮箱：325927775@qq.com
 * 说明：
 */
public class FileHelper {

    public static File getSavedFile(String appendix) {
        File appDir = new File(PathUtils.getExternalPicturesPath(), Utils.getApp().getString(R.string.app_name));
        FileUtils.createOrExistsDir(appDir.getPath());
        String fileName = System.currentTimeMillis() + "." + appendix;
        return new File(appDir, fileName);
    }

    /*fun saveImageToGallery(context: Context, file: File, fileName: String) {
        try {
            MediaStore.Images.Media.insertImage(context.contentResolver, file.absolutePath, fileName, null)
        } catch (e: FileNotFoundException) {
            L.d("saveImageToGallery: FileNotFoundException MediaStore")
            e.printStackTrace()
        }
        context.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, getUriFromFile(context, file)))
    }
*/

    public static void saveImageToGallery(Context context, File file, String fileName) {
        try {
            MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, getUriFromFile(context, file)));
    }

    public static Uri getUriFromFile(Context context, File file) {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            return FileProvider.getUriForFile(context, "com.team.childapp" + ".fileProvider", file);
        } else {
            return Uri.fromFile(file);
        }
    }

    public static String getExternalPicturesPath() {
        if (isExternalStorageDisable()) {
            return "";
        } else {
            return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath();
        }
    }

    private static boolean isExternalStorageDisable() {
        return Environment.MEDIA_MOUNTED != Environment.getExternalStorageState();
    }

    /**
     * 文件转base64
     * @param file
     * @return
     */
    public static String encodeFileToBase64(File file) {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            byte[] fileBytes = new byte[(int) file.length()];
            fileInputStream.read(fileBytes);
            return Base64.encodeToString(fileBytes, Base64.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    // 计算本地文件路径对应的图片大小
    public static long calculateImageSize(String filePath) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);

        int width = options.outWidth;
        int height = options.outHeight;

        // 计算每个像素所占字节数，默认为 ARGB_8888 格式，即 4 字节
        int bytesPerPixel = 4;

        // 计算总大小
        long sizeInBytes= (long)width * height * bytesPerPixel;

        return sizeInBytes;
    }


}